<?php
include("const.php");
if (!defined("VHOSTS_PATH")) {
    echo "请配置vhosts绝对路径";
    exit;
}
if (!file_exists(VHOSTS_PATH)) {
    echo "vhosts路径不存在";
    exit;
}
$dir = VHOSTS_PATH;
if (isset($_POST['Action']) && $_POST['Action'] == 'delete') {
    $del_dir = $dir . "./../del_conf/";
    if (!file_exists($del_dir)) {
        mkdir($del_dir, 0777, true);
    }
    $fileName = str_replace("-", ".", $_POST['file']);
    copy($dir . $fileName, $del_dir . $fileName);
    unlink($dir . $fileName);
    shell_exec(RELOAD_NGINX_CMD);
    header("Location: index.php");
}
if (!isset($_GET['reloadFile'])) {
    $reloadFile = "";
} else {
    $reloadFile = $_GET['reloadFile'];
}
$handle = opendir($dir);
$array_file = array();
while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
        $array_file[] = $file; //输出文件名
    }
}
sort($array_file);
closedir($handle);
?>
<style>
    @font-face {
        font-family: 'JetBrainsMono-SemiLight-Italic';
        src: url('./font/JetBrainsMono/web/woff/JetBrainsMono-SemiLight-Italic.woff') format("woff");
    }

    * {
        font-family: "JetBrainsMono-SemiLight-Italic"
    }

    td {
        vertical-align: top;
    }

    .focus {
        background: black;
        color: white
    }

    table, tr, td {
        height: 100%
    }

    a {
        display: inline-block;
        margin-top: 5px;
        padding: 3px 10px 3px 3px;
        width: 250px;
        border-radius: 5px;
        text-decoration: none;
        background: #c9c9c9;
        color: black
    }
</style>
<body>
<table>
    <tr>
        <td width="30%" align=right>
            <?php if (IS_REMOTE) { ?>
                <a href="remote.php">jump to the remote.php</a><br>
            <?php } ?>
            <a href="javascript:;" id="add-new-conf" onclick="add_new_conf(this)">add new conf</a><br>
            <?php foreach ($array_file as $v) { ?>
                <a href="javascript:;" id="<?php echo str_replace(".", "-", $v); ?>"
                   url="./detail.php?file=<?php echo $v; ?>" onclick="getInfo(this)" onmousedown="holdDown(this)"
                   onmouseup="holdUp()"><?php echo $v; ?></a><br>
            <?php } ?>
        </td>
        <td style="padding:10px">
            <iframe id="box" name="box"
                    style="border:1px #c9c9c9 dashed;border-radius:10px;width:1000px;height:100%;"></iframe>
        </td>
        <form method="post">
            <input type="hidden" id="file" name="file">
            <input type="hidden" id="Action" name="Action" value="delete">
        </form>
    </tr>
</table>
</body>
<script>
    let timeStart, timeEnd, time;//申明全局变量
    function getInfo(obj) {
        let url = obj.getAttribute("url");
        document.querySelector("#box").setAttribute("src", url);
        document.querySelector(".focus") ? document.querySelector(".focus").setAttribute("class", "") : "";
        obj.setAttribute("class", "focus");
    }

    document.querySelector("#box").setAttribute("src", "./detail.php?file=<?php echo $reloadFile; ?>");
    if ("<?php echo $reloadFile; ?>" !== "") {
        document.querySelector("#<?php echo str_replace(".", "-", $reloadFile); ?>").setAttribute("class", "focus");
    } else {
        document.querySelector("#add-new-conf").setAttribute("class", "focus");
    }

    function getTimeNow()//获取此刻时间
    {
        let now = new Date();
        return now.getTime();
    }

    function holdDown(_self)//鼠标按下时触发
    {
        timeStart = getTimeNow();//获取鼠标按下时的时间
        time = setInterval(function ()//setInterval会每100毫秒执行一次
        {
            timeEnd = getTimeNow();//也就是每100毫秒获取一次时间
            if (timeEnd - timeStart > 1000)//如果此时检测到的时间与第一次获取的时间差有1000毫秒
            {
                clearInterval(time);//便不再继续重复此函数 （clearInterval取消周期性执行）
                let rs = confirm("长按移除配置文件，" + _self.innerHTML + "文件移动到<?php echo VHOSTS_PATH; ?>./../del_conf/'文件夹中，是否删除？");
                if (rs) {
                    let fileName = _self.id;
                    document.querySelector("#file").value = fileName;
                    document.forms[0].submit();
                }
            }
        }, 100);
    }

    function holdUp() {
        clearInterval(time);//如果按下时间不到1000毫秒便弹起，
    }

    function add_new_conf(obj) {
        document.querySelector("#box").setAttribute("src", "./detail.php?file=");
        document.querySelector(".focus") ? document.querySelector(".focus").setAttribute("class", "") : "";
        obj.setAttribute("class", "focus");
    }

    function moveFocus(flg) {
        let current = document.querySelector('.focus');
        let elements = Array.prototype.filter.call(current.parentNode.children, (v, k) => {
            return v.nodeName === "A";
        });
        for (let i = 0; i < elements.length; i++) {
            if (elements[i].className === 'focus') {
                if (flg === 1) {
                    if (i === elements.length - 1) {
                        elements[1].click();
                    } else {
                        elements[i + 1].click();
                    }
                } else {
                    if (i === 1) {
                        elements.pop().click();
                    } else {
                        elements[i - 1].click();
                    }
                }
                break;
            }
        }
    }

    document.body.onkeydown = (e) => {
        e = e || event || window.event;
        let keyCode = e.keyCode;
        if (keyCode === 40) {
            moveFocus(1);
        } else if (keyCode === 38) {
            moveFocus(-1)
        }
    };
</script>